/**
 * Created by yihua on 2017/3/7.
 */

$(document).ready(function () {
    $(".del_note").each(function () {
        var id = $(this).parent().parent().find("td").eq(0).text();

        $(this).click(function () {
            console.log(id)
            $("#deleteModal").modal("show");
            $("#delete_confirm").unbind('click').click(function () {
                console.log(id)
                data = {
                    'note_id': id,
                    'type': 'delete'
                };
                editNote(data)

            });
        });
    });
    $("#editcategoryModal").on('shown.bs.modal', function (e) {
        $("#cir_type").select2();
    })
    $(".edit_category").each(function () {
        $(this).click(function () {
            var id = $(this).parent().parent().find("td").eq(0).text();
            console.log(id)
            $("#editcategoryModal").modal("show");
            $("#edit_confirm").unbind('click').click(function () {
                var types = '';
                $("input[name=type]:checked").each(function (i) {
                    if (0 == i) {
                        types = $(this).val();
                    } else {
                        types += ("," + $(this).val());
                    }
                });
                console.log('types: ', types);
                data = {
                    'note_id': id,
                    'type': types,
                };
                editNote(data);
            });

        });
    });

    function editNote(data) {
        $.ajax({
            url: '/note/updatenote',
            timeout: 100000,
            type: 'get',
            dataType: 'json',
            data: data,
            success: function (rs) {
                if (rs.errno != 0) {
                    toastr.error(rs.errmsg, '', {positionClass: 'toast-bottom-center'});
                } else {
                    toastr.success(rs.errmsg);
                    window.location.reload();
                }
            },
            error: function () {
                toastr.error('系统错误', '', {positionClass: 'toast-bottom-center'});
            }
        });
    }
});